/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.HashMap;
import java.util.Iterator;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.access.RequestParameter;
import com.elf.gwt.common.client.callback.TableNavigatorWidgetCallback;
import com.elf.gwt.common.client.model.EditTableMetaModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.common.client.widget.TableNavigatorWidget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Widget;

/**
 *  
 */
public class EditCellTableComponent extends JSONServerComponent
{	
	protected static final String Update = "Update";
	protected int selectedGroupId = 0;
	protected String action = "";
	protected boolean edit = false;
	protected boolean editable = true;
	protected HashMap<String, Widget> elements = null;

	protected HorizontalPanel buttonPanel = null;
	protected Button editButton;
	protected Button updateButton;
	protected Button cancelButton;

	protected int numberOfTableEntries = 0;
	protected int numberOfTableColumns = 0;

	protected String tableType = "records";
	protected int extraRows = 5;
	protected boolean paginate = false;
	protected boolean localPaginate = false;
	protected int maxRows = 20;
	protected int offset = 0;

	protected EditTableMetaModel editTableMetaModel = null;
	protected TableNavigatorWidget tableNavigatorWidget = null;

	/**
	 * Constructor
	 */
	public EditCellTableComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		setupTimer();
		edit = false;
		editable = true;
		elements = new HashMap<String, Widget>();
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		if (params.length > 1) selectedGroupId = Integer.parseInt(params[1]);
		edit=false;

		return super.updateFromHistoryParameters(params);
	}

	public void additionalRequestParameters()
	{
		addRequestParameter(new RequestParameter("edit", edit));
		addRequestParameter(new RequestParameter("action", action));
		addRequestParameter(new RequestParameter("paginate", paginate));
		if (paginate) {
			addRequestParameter(new RequestParameter("max_rows", maxRows));
			addRequestParameter(new RequestParameter("offset", offset));
		}

		if (action != null && (action.equals(Update)))
		{
			Iterator<String> iterElements = elements.keySet().iterator();
			while (iterElements.hasNext())
			{
				String key = iterElements.next();
				Widget widget = elements.get(key);
				String value = CommonUtils.getValue(widget);
				if (value != null && value.length() > 0) addRequestParameter(new RequestParameter(key, value));
			} 
			edit = false;
		}
		action = "";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		if (paginate) {
			editTableMetaModel = new EditTableMetaModel(jsonObject);
		}
	}

	public void onBuilt()
	{
		setVisible(!controller.isInitializing());
	}

	public void buildFromModel() throws Exception
	{
		clear();
		elements.clear();
		if (editable) {
			//Buttons
			updateButtonPanel();
			add(buttonPanel);
			add(new InlineHTML("<br/><br/><br/>"));
		}
		else
		{
			add(new InlineHTML("<br/>"));
		}

		add(getTable());

		onBuilt();
	}
	
	public CellTable<Object>  getTable() {
		return null;
	}

	protected ClickHandler editHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			edit = true;
			action = "";
			build(true);
		}
	};

	protected ClickHandler cancelHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			edit = false;
			action = "";
			build(true);
		}
	};

	protected ClickHandler updateHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			action = Update;
			build(true);
		}
	};

	public class EditTableNavigatorCallback implements TableNavigatorWidgetCallback
	{
		public void onSelection(int change_offset)
		{
			offset = change_offset;
			edit = false;
			build(true);
		}

		public void onOrderby(String orderby)
		{
		}
	}

	public void updateButtonPanel() {
		if (buttonPanel == null)
		{
			buttonPanel = new HorizontalPanel();
			buttonPanel.setStyleName("ZButtons");

			editButton = new Button("edit", editHandler);
			editButton.addStyleName("ComponentLink");
			buttonPanel.add(editButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			cancelButton = new Button("cancel", cancelHandler);
			cancelButton.addStyleName("ComponentLink");
			buttonPanel.add(cancelButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));

			updateButton = new Button("update", updateHandler);
			updateButton.addStyleName("ComponentLink");
			buttonPanel.add(updateButton);
			buttonPanel.add(new InlineHTML("&nbsp;&nbsp;"));
		}
		editButton.setVisible(!edit);
		cancelButton.setVisible(edit);
		updateButton.setVisible(edit);
	}
}
